'''
Created on 2009-11-11

@author: Jialai_Zhu
'''
from multiprocessing import Process, Lock
import multiprocessing
import threading

class lock_entry(object):
    def __init__(self,lock,id):
        self.lock=lock
        self.id=id
        return
    
def do_time_func(id):        
    print 'timer_func'
    global process_lock_list 
    process_lock_list.at(id).lock.Release()
    global t
    t.run()
def timer_func(id):
    do_time_func(id)
#    try:
#        print id
#        do_time_func(id)
#    except:
#        pass


        
class lock_list(object):
    def __init__(self):
        self.list=[]
        self.id=0
    def new(self):        
        self.id+=1        
        l=lock_entry(Lock(),len(do_time_func(id))
        self.list.append(l)
        return l
    def at(self,id):
        return self.list[id]
    
global process_lock_list    
process_lock_list=lock_list()   
       
def f(l,hanlde):
    print 'def f(l):'
    while 1:
        l.acquire()
        hanlde()
        print 'hello world'
    print 'end'
    
        
def fork(hanlde):
    lock_entry = process_lock_list.new()
    lock=lock_entry.lock
#    for num in range(10):
    p=Process(target=f, args=(lock,hanlde,))
    p.daemon=True
     

#    Process.current_process().daemon=True 
    lock.acquire()
    print '    lock.acquire()'
    p.start()
    global t        #Notice: use global variable!
    t = threading.Timer(5.0, timer_func,args=(lock_entry.id,))    
    t.start()
    try:  
        p.join()
    except:
        print "xxxxxx"
        t.cancel()
        print p.terminate()    
        exit()   
         
def child_handle():
    print 'child_handle'
    
fork(child_handle)        